package com.wslNB.openfeign.controller;



import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wslNB.openfeign.domain.GodWSL;
import com.wslNB.openfeign.mapper.GodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/provider")
public class ProviderController {


    @Autowired
    private GodMapper godMapper;


    /**
     * 降级：
     *  1. 出现异常
     *  2. 服务调用超时
     *      * 默认1s超时
     *
     *  @HystrixCommand(fallbackMethod = "findOne_fallback")
     *      fallbackMethod：指定降级后调用的方法名称
     */


    @GetMapping("/findOne/{id}")
    @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {
            //设置Hystrix的超时时间，默认1s
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
    })
    public GodWSL findOne(@PathVariable("id") Integer id){

        int i = 1/0;

        GodWSL godWSL = godMapper.findOne(id);


        godWSL.setTitle(godWSL.getTitle() + ":" + id);
        return godWSL;
    }


    /**
     * 定义降级方法：
     *  1. 方法的返回值需要和调用方法一样
     *  2. 方法的参数（个数和类型）需要和调用方法一样
     */
    public GodWSL findOne_fallback(Integer id){

        GodWSL godWSL = new GodWSL();
        godWSL.setTitle("系统维护中……");
        return godWSL;
    }





}
